<?php
namespace app\controllers;

use app\models\Catalogue;
use yii\base\Controller;

class CatalogueController extends Controller{

    //新建目录
    public function actionCreate(){

        $request = \Yii::$app->request;

        $catalogue = new Catalogue();//实例化
        $catalogueName = $request ->post('catalogue_name','新建目录');
        $catalogue->catalogue_name = $catalogueName;
//        trim($catalogueName);//去掉$catalogueName里的空格，但是不知道为什么不行
        if ($catalogue->find()->where(['catalogue_name' => $catalogueName])->one()) {
            return $this->error("catalogueName_Is_Exist",1);
        }
        else if (empty($catalogueName)) {
            return $this->error("Catalogue_Name_Cannot_Be_Empty",2);
        }
        else
            $catalogue -> insert();
            return $this -> success("New_Catalogue_Created_Successfully");

    }

    //目录列表
    public function actionList(){

        $result = Catalogue::find()->asArray()->all();
        return $this -> json($result);

    }

    //错误报告
    public function error($message,$errorId){
        return json_encode(['message'=>$message,'errorId'=>$errorId]);
    }

    //目录创建成功
    public function success($message){
        return json_encode(['message'=>$message]);
    }


}